<!-- 合同金额排行 -->
<template>
  <!-- 柱状图 -->
  <el-card shadow="never">
    <el-skeleton :loading="loading" animated>
      <Echart :height="500" :options="echartsOption" v-if="!isData" />
      <div style="height: 500px; display: flex; align-items: center; justify-content: center" v-else
        ><el-empty :image="emptyImg" :image-size="200" description="暂无数据"></el-empty
      ></div>
    </el-skeleton>
  </el-card>

  <!-- 排行列表 -->
  <el-card shadow="never" class="mt-16px">
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="公司排名" align="center" type="index" width="100" />
      <el-table-column label="员工" align="center" prop="nickname" min-width="200" />
      <el-table-column label="部门" align="center" prop="deptName" min-width="200" />
      <el-table-column label="数量" align="center" prop="count" min-width="200" sortable />
    </el-table>
  </el-card>
</template>
<script setup lang="ts">
import { StatisticsRankApi } from '@/api/call/statistics/index'
import { EChartsOption } from 'echarts'
import { clone } from 'lodash-es'
import emptyImg from '@/assets/img/empty.png'

defineOptions({ name: 'CallRank' })
const props = defineProps<{ queryParams: any }>() // 搜索参数

const isData = ref(false)
const loading = ref(false) // 加载中
const list = ref<StatisticsRankRespVO[]>([]) // 列表的数据

/** 柱状图配置：横向 */
const echartsOption = reactive<EChartsOption>({
  dataset: {
    dimensions: ['nickname', 'count'],
    source: []
  },
  grid: {
    left: 50,
    right: 120,
    bottom: 20,
    containLabel: true
  },
  legend: {
    top: 20
  },
  series: [
    {
      name: '接通数量排行',
      type: 'bar'
    }
  ],
  toolbox: {
    feature: {
      dataZoom: {
        yAxisIndex: false // 数据区域缩放：Y 轴不缩放
      },
      brush: {
        type: ['lineX', 'clear'] // 区域缩放按钮、还原按钮
      },
      saveAsImage: { show: true, name: '接通数量排行' } // 保存为图片
    }
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  xAxis: {
    type: 'category',
    name: '员工'
  },
  yAxis: {
    type: 'value',
    name: '接通数量'
  }
}) as EChartsOption

/** 获取合同金额排行 */
const loadData = async () => {
  // 1. 加载排行数据
  loading.value = true
  const rankingList = await StatisticsRankApi.getSuccessCountRank(props.queryParams)
  isData.value = rankingList.length == 0
  // 2.1 更新 Echarts 数据
  if (echartsOption.dataset && echartsOption.dataset['source']) {
    echartsOption.dataset['source'] = clone(rankingList).reverse()
  }
  // 2.2 更新列表数据
  list.value = rankingList
  loading.value = false
}
defineExpose({ loadData })

/** 初始化 */
onMounted(() => {
  loadData()
})
</script>
